﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSystems.Data.Entities;


namespace OneSystems.BusinessLogic
{
    public sealed class TransactionLogic
    {
        private TransactionLogic() { }

        #region GetTransactionHistory
        public static DataTable GetTransactionHistory(int accountID)
        {
            return GetTransactionHistory(accountID, DateTime.MinValue, DateTime.MaxValue);
        }
        public static DataTable GetTransactionHistory(int accountID, DateTime startDate)
        {
            return GetTransactionHistory(accountID, startDate, DateTime.MaxValue);
        }
        public static DataTable GetTransactionHistory(int accountID, DateTime startDate, DateTime endDate)
        {
            DataTable dt = new DataTable("AccountHistory");
            DataColumn[] dc = { new DataColumn("Reconciled", typeof(bool)), 
                                  new DataColumn("Occurred", typeof(DateTime)), 
                                  new DataColumn("Transaction", typeof(string)), 
                                  new DataColumn("Amount", typeof(decimal)), 
                                  new DataColumn("Balance", typeof(decimal)) };
            dt.Columns.AddRange(dc);

            decimal balance = 0;

            Account account = Account.GetOne(accountID);

            foreach (Transaction transaction in account.Transactions.Where(c=>c.Occurred > startDate && c.Occurred < endDate).OrderBy(c=>c.Occurred))
            {
                DataRow row = dt.NewRow();
                row["Reconciled"] = transaction.Reconciled;
                row["Occurred"] = transaction.Occurred;
                row["Transaction"] = transaction.TransactionType.Name;
                row["Amount"] = transaction.Amount;
                if (transaction.TransactionType.Operation == '+')
                {
                    balance += transaction.Amount;
                }
                else
                {
                    balance -= transaction.Amount;
                }
                row["Balance"] = balance;
                dt.Rows.Add(row);

            }


            return dt;
        }
        #endregion

        #region GetNormalPaymentChart
        public static DataTable GetNormalPaymentChart(int accountID)
        {
            return GetNormalPaymentChart(accountID, 0);
        }
        //[Obsolete("Use the other method instead", true)]
        public static DataTable GetNormalPaymentChart(int accountID, decimal pledgeAmount)
        {
            DataTable dt = new DataTable("PaymentChart");
            DataColumn[] dc = { new DataColumn("Date", typeof(DateTime)),
                new DataColumn("Description", typeof(string)),
                new DataColumn("Interest", typeof(decimal)),
                new DataColumn("Payment", typeof(decimal)),
                new DataColumn("Balance", typeof(decimal))};
            dt.Columns.AddRange(dc);
            Account account = Account.GetOne(accountID);


            decimal balance = account.CurrentBalance;
            decimal interest = 0;
            decimal payment = 0;
            DateTime dueDay = account.NextDueDate;


            while (balance > 0)
            {
                if (balance < account.MinimumPayment)
                {
                    payment = balance;
                }
                else
                {
                    payment = balance * account.PaymentPercentage;
                    if (payment < account.MinimumPayment)
                    {
                        payment = account.MinimumPayment;
                    }
                }
                payment = Math.Round(payment, 0);
                payment += pledgeAmount;
                interest = Math.Round(balance * account.InterestRate / 12, 2);
                balance = balance += interest;
                balance = balance -= payment;
                if (balance < 0)
                {
                    balance = 0;
                }
                DataRow row = dt.NewRow();
                row["Date"] = dueDay;
                row["Description"] = account.Nickname;
                row["Interest"] = interest;
                row["Payment"] = payment;
                row["Balance"] = Math.Round(balance, 2);
                dt.Rows.Add(row);

                dueDay = dueDay.AddMonths(1);

            }
            return dt;
        }

        public static List<Transaction> GetFuturePayments(int accountID)
        {
            Account account = Account.GetOne(accountID);
            return account.FutureTransactions;
        }

        public static DataTable GetNormalChart(int accountID, decimal pledgeAmount)
        {
            DataTable dt = new DataTable("PaymentChart");
            DataColumn[] dc = { new DataColumn("Date", typeof(DateTime)),                
                new DataColumn("Balance", typeof(decimal))};
            dt.Columns.AddRange(dc);
            Account account = Account.GetOne(accountID);


            decimal balance = account.CurrentBalance;
            decimal interest = 0;
            decimal payment = 0;
            DateTime dueDay = account.NextDueDate;


            while (balance > 0)
            {
                if (balance < account.MinimumPayment)
                {
                    payment = balance;
                }
                else
                {
                    payment = balance * account.PaymentPercentage;
                    if (payment < account.MinimumPayment)
                    {
                        payment = account.MinimumPayment;
                    }
                }
                payment = Math.Round(payment, 0);
                payment += pledgeAmount;
                interest = Math.Round(balance * account.InterestRate / 12, 2);
                balance = balance += interest;
                balance = balance -= payment;
                if (balance < 0)
                {
                    balance = 0;
                }
                DataRow row = dt.NewRow();
                row["Date"] = dueDay;
                row["Balance"] = Math.Round(balance, 2);
                dt.Rows.Add(row);

                dueDay = dueDay.AddMonths(1);

            }
            return dt;
        }
        public static DataTable GetReductionPlan(int debtorID, decimal pledgeAmount)
        {
            Debtor debtor = Debtor.GetOne(debtorID);
            decimal startingBalance = debtor.TotalDebtBalance;


            DataTable dt = new DataTable("DebtReductionPlan");

            while (startingBalance > 0)
            {

            }

            return dt;
        }
        #endregion

        #region GetPaymentPlan

        #endregion

    }
}
